package com.tianqu.tms.www.finance.controller;

import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.core.webservice.weichat.api.MobileMessage;
import com.tianqu.tms.core.webservice.weichat.api.PullingExecutor;
import com.tianqu.tms.core.webservice.weichat.api.rpc.TransferDataSyn11000Bo;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.entity.Sequence;
import com.tianqu.tms.entity.TransferAcceptedInfo;
import com.tianqu.tms.entity.TransferFinanceInfo;
import com.tianqu.tms.entity.TransferPersonalCarInfo;
import com.tianqu.tms.entity.TransferProcessHistory;
import com.tianqu.tms.www.finance.actionform.CarProxyPaySearchInputForm;

public class CarProxyPayBackResultController extends TmsController {

	@Override
	public void execute(UserContext uc) throws Exception {
		TxController controller = TxController.getTxController();
		Sequence seq = Sequence.getInstance();

		// 添加事务提交异常捕获
		try {
			controller.begin();
			CarProxyPaySearchInputForm inputForm = (CarProxyPaySearchInputForm) uc.getInputForm();
			TmsUserProfile profile = uc.getUserProfile();
			String serial = inputForm.getSerial();
			TransferAcceptedInfo accept = new TransferAcceptedInfo();
			accept = accept.getAcceptInfoBySerial(serial,"");
			TransferFinanceInfo finance = new TransferFinanceInfo();
			finance = finance.findFinanceInfoByserial(serial);
			TransferProcessHistory process = new TransferProcessHistory();
			//如果车辆是库存车辆
			if(accept.getMarketType() == 1)
			{
				Car car = new Car();
				//modify by huangheli at 20151016 start
				car.setStatus(1);
				//car.setStatus(4);
				//car.setRegisterStatus(4);
				//modify by huangheli at 20151016 end
				car.setSerial(accept.getBasicCarId());
				car.setUpdateUserId(profile.getUniqueId());
				//modify by huangheli at 20151016 start
				//car.updateCarStatus(car);
				car.updateCarStatusOnly(car);
				//modify by huangheli at 20151016 end
			}
			//如果车辆是非库存车辆
			if(accept.getMarketType() == 2)
			{
				TransferPersonalCarInfo person = new TransferPersonalCarInfo();
				person.setSerial(accept.getBasicCarId());
				person.setStatus(4);
				person.setUpdateUserId(profile.getUniqueId());
				person.updateCarStatus(person);
			}
			accept.setStatus(16);
			accept.setSerial(serial);
			accept.updateAcceptStatusBySerial(accept);
			
			//workload:3.1.2
			//add by huangheli at 20151016 start
			//插入 过户业务基本信息表（tms_transfer_accepted_info）
//			String transferSerial = seq.getSerial(6);
//			TransferAcceptedInfo transferAcceptedInfo = TransferAcceptedInfo.getInstance();
//			transferAcceptedInfo.setSerial(transferSerial);
//			transferAcceptedInfo.setMarketType(1);
//			transferAcceptedInfo.setCarClass(2);
//			transferAcceptedInfo.setStatus(0);
//			transferAcceptedInfo.setSourceType(1);
//			transferAcceptedInfo.setLicensePlate(accept.getLicensePlate());
//			transferAcceptedInfo.setBasicCarId(accept.getBasicCarId());
//			transferAcceptedInfo.setMemberId(accept.getMemberId());
//			transferAcceptedInfo.setInsertUserid(profile.getUniqueId());
//			transferAcceptedInfo.setUpdateUserId(profile.getUniqueId());
//			transferAcceptedInfo.insertCarResumeInfo(transferAcceptedInfo);
			
			//add by huangheli at 20151016 end
			process.setSerial(serial);
			process.setBusinessType(42);
			process.setReason(inputForm.getReason());
			process.setInsertUserId(profile.getUniqueId());
			process.insertHisInfoAtAssess(process);
//			if(inputForm.getCheckPriceChargeType() == 1){
//				finance.setCheckBackPrice(Integer.parseInt(inputForm.getCheckBackPrice()));
//			}else{
//				finance.setCheckBackPrice(Integer.parseInt(inputForm.getCardScore()));
//			}
			finance.setCheckBackPrice(Integer.parseInt(inputForm.getCheckBackPrice()));
		    finance.setSerial(serial);
		    finance.setCheckBackPriceUserId(profile.getUniqueId());
		    finance.setCheckBackChargeType(inputForm.getCheckPriceChargeType());
		    finance.updateTransferCheckBackPrice(finance);
//			if(inputForm.getCheckPriceChargeType() == 1)
//			{
//				IntegralRegister reg = new IntegralRegister();
//				reg = reg.getIntegralRegisterInfoBySerial(serial, 1, 1);
//				if(reg!=null)
//				{
//					if(reg.getMemberType() == 1)
//					{
//						Member member = new Member();
//						member = member.findCarScoreByMemberId(reg.getMemberId());
//						member.setCardScore(member.getCardScore()-reg.getIntegral());
//						member.setMemId(Integer.parseInt(reg.getMemberId()));
//						member.setUpdateUserId(profile.getUniqueId());
//						member.updateIntegralCardScore(member);
//					}
//					if(reg.getMemberType() == 2)
//					{
//						Attn attn = new Attn();
//						attn = attn.findAttnByAttnId(reg.getMemberId());
//						attn.setCardScore(attn.getCardScore()-reg.getIntegral());
//						attn.setAttnId(reg.getMemberId());
//						attn.setUpdateUserId(profile.getUniqueId());
//						attn.updateIntegralCardScore(attn);
//					}
//					
//					reg.setMemberId(reg.getMemberId());
//					reg.setMemberType(reg.getMemberType());
//					reg.setSerial(serial);
//					reg.setLicensePlate(reg.getLicensePlate());
//					reg.setTransferPrice(Integer.parseInt(StringUtil.removeDH(inputForm.getCheckBackPrice())));
//					reg.setInsertUserId(profile.getUniqueId());
//					reg.setPlusMinus(2);
//					reg.setChargeType(3);
//					reg.setIntegral(reg.getIntegral());	
//					reg.insertRegisterInfo(reg);
//				}
//			}else{
//					//返回积分
//					int sb1 = Integer.parseInt(inputForm.getCardScore());
//					IntegralRegister register = new IntegralRegister();
//					//扣分
//					if("1".equals(inputForm.getMemType()))
//					{
//						Member member = new Member();
//						member = member.findCarScoreByMemberId(inputForm.getMemberId());
//						member.setCardScore(member.getCardScore()+sb1);
//						member.setMemId(Integer.parseInt(inputForm.getMemberId()));
//						member.setUpdateUserId(profile.getUniqueId());
//						member.updateIntegralCardScore(member);
//					}
//					if("2".equals(inputForm.getMemType()))
//					{
//						Attn attn = new Attn();
//						attn = attn.findAttnByAttnId(inputForm.getMemberId());
//						attn.setCardScore(attn.getCardScore()+sb1);
//						attn.setAttnId(inputForm.getMemberId());
//						attn.setUpdateUserId(profile.getUniqueId());
//						attn.updateIntegralCardScore(attn);
//					}
//					register.setMemberId(inputForm.getMemberId());
//					register.setMemberType(Integer.parseInt(inputForm.getMemType()));
//					register.setSerial(serial);
//					register.setLicensePlate(accept.getLicensePlate());
//					register.setTransferPrice(sb1);
//					register.setInsertUserId(profile.getUniqueId());
//					register.setPlusMinus(1);
//					register.setChargeType(3);
//					register.setIntegral(sb1);	
//					register.insertRegisterInfo(register);
//			}
			// add by huangheli 2016/01/09 start
			MobileMessage message = new MobileMessage();
			message.putValue("Serial", serial);
			message.setMessage("财务模块[过户收费]接口,过户流水号为#Serial#过户信息不存在，无法同步TMS的流程表退费");
			TransferDataSyn11000Bo bo = new TransferDataSyn11000Bo();
			bo.setMobileMessage(message);
			bo.setSerial(serial);
			bo.setFunc(17);
//			workload:3.1.2 delete
//			//如果车辆是库存车辆
//			if(accept.getMarketType() == 1){
//				bo.setStatus(1);
//				bo.setDetailStatus(0);	
//			}
//			//如果车辆是非库存车辆
//			if(accept.getMarketType() == 2){
				bo.setStatus(16);
				bo.setDetailStatus(42);	
//			}
			PullingExecutor.getInstance().submit(bo);	
			// add by huangheli 2016/01/09 end
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				try {
					throw e;
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}
	}
}
